package cbfg.butil.demo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import cbfg.butil.KLog;

/**
 * @author: Tom Hawk
 * 添加时间: 2020/9/4 17:32
 * 功能描述:
 */
public class KLogFragment extends Fragment implements View.OnClickListener {
    private static final String LOG_MSG = "KLog is a so cool Log Tool!";
    private static final String TAG = "KLog";
    private static String XML = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><!--  Copyright w3school.com.cn --><note><to>George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting!</body></note>";
    private static String JSON;
    private static String JSON_LONG;
    private static String STRING_LONG;

    private static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            KLog.d("Inner Class Test");
        }
    };

    public KLogFragment() {
        super(R.layout.fragment_klog);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.btnLogDebug).setOnClickListener(this);
        view.findViewById(R.id.btnLog).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithNull).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithMsg).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithLong).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithTag).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithParams).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithJson).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithLongJson).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithJsonTag).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithFile).setOnClickListener(this);
        view.findViewById(R.id.btnLogWithXml).setOnClickListener(this);

        JSON_LONG = getResources().getString(R.string.json_long);
        JSON = getResources().getString(R.string.json);
        STRING_LONG = Utils.getLongTxt(view.getContext());

        handler.sendEmptyMessageDelayed(0, 3000);
    }

    private void logDebug() {
        KLog.d("This is a debug message");
        KLog.d("DEBUG", "params1", "params2", this);
    }

    private void log() {
        TestKLog.v();
        TestKLog.d();
        TestKLog.i();
        TestKLog.w();
        TestKLog.e();
        TestKLog.a();
    }

    private void logWithNull() {
        KLog.v(null);
        KLog.d(null);
        KLog.i(null);
        KLog.w(null);
        KLog.e(null);
        KLog.a(null);
    }

    private void logWithMsg() {
        KLog.v(LOG_MSG);
        KLog.d(LOG_MSG);
        KLog.i(LOG_MSG);
        KLog.w(LOG_MSG);
        KLog.e(LOG_MSG);
        KLog.a(LOG_MSG);
    }

    private void logWithTag() {
        KLog.v(TAG, LOG_MSG);
        KLog.d(TAG, LOG_MSG);
        KLog.i(TAG, LOG_MSG);
        KLog.w(TAG, LOG_MSG);
        KLog.e(TAG, LOG_MSG);
        KLog.a(TAG, LOG_MSG);
    }

    private void logWithLong() {
        KLog.d(TAG, STRING_LONG);
    }

    private void logWithParams() {
        KLog.v(TAG, LOG_MSG, "params1", "params2", this);
        KLog.d(TAG, LOG_MSG, "params1", "params2", this);
        KLog.i(TAG, LOG_MSG, "params1", "params2", this);
        KLog.w(TAG, LOG_MSG, "params1", "params2", this);
        KLog.e(TAG, LOG_MSG, "params1", "params2", this);
        KLog.a(TAG, LOG_MSG, "params1", "params2", this);
    }

    private void logWithJson() {
        KLog.json("12345");
        KLog.json(null);
        KLog.json(JSON);
    }

    private void logWithLongJson() {
        KLog.json(JSON_LONG);
    }

    private void logWithJsonTag() {
        KLog.json(TAG, JSON);
    }

    private void logWithFile(View view) {
        KLog.file(null, JSON_LONG, view.getContext().getExternalCacheDir(), null, true);
        KLog.file(TAG, JSON_LONG, view.getContext().getExternalCacheDir(), null, true);
        KLog.file(TAG, JSON_LONG, view.getContext().getExternalCacheDir(), "test.txt", true);
    }

    private void logWithXml() {
        KLog.xml("12345");
        KLog.xml(null);
        KLog.xml(XML);
    }

    @Override
    public void onClick(View view) {
        int vid = view.getId();

        if (vid == R.id.btnLogDebug) {
            logDebug();
            return;
        }
        if (vid == R.id.btnLog) {
            log();
            return;
        }
        if (vid == R.id.btnLogWithNull) {
            logWithNull();
            return;
        }
        if (vid == R.id.btnLogWithMsg) {
            logWithMsg();
            return;
        }
        if (vid == R.id.btnLogWithLong) {
            logWithLong();
            return;
        }
        if (vid == R.id.btnLogWithTag) {
            logWithTag();
            return;
        }
        if (vid == R.id.btnLogWithParams) {
            logWithParams();
            return;
        }
        if (vid == R.id.btnLogWithJson) {
            logWithJson();
            return;
        }
        if (vid == R.id.btnLogWithLongJson) {
            logWithLongJson();
            return;
        }
        if (vid == R.id.btnLogWithJsonTag) {
            logWithJsonTag();
            return;
        }
        if (vid == R.id.btnLogWithFile) {
            logWithFile(view);
            return;
        }
        if (vid == R.id.btnLogWithXml) {
            logWithXml();
        }
    }
}
